package com.example.a20112.ui_demo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.a20112.ui_demo.beans.LoginBean;
import com.example.a20112.ui_demo.listener.RetrofitListener;
import com.example.a20112.ui_demo.model.UserModel;

public class LoginActivity extends AppCompatActivity implements RetrofitListener<LoginBean> {
    private EditText usernameview, passwordview;
    private Button loginButton;
    private Button registerButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initEvent();
    }

    private void initViews() {
        usernameview = (EditText) findViewById(R.id.editText);
        passwordview = (EditText) findViewById(R.id.editText2);
        loginButton = (Button) findViewById(R.id.button);
        registerButton = (Button) findViewById(R.id.registerbutton);



    }

    private void initEvent() {

      loginButton.setOnClickListener(new View.OnClickListener(){

          @Override
          public void onClick(View v) {
            if(usernameview.getText().toString().trim().equals("")||passwordview.getText().toString().trim().equals("")) {
                 Toast.makeText(LoginActivity.this,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
              }
              else{
                 login();
              }
          }
      });
      registerButton.setOnClickListener(new View.OnClickListener(){
          @Override
          public void onClick(View v) {
                         Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                         startActivityForResult(intent,1);

          }
      });
    }


    private void login() {
        UserModel userModel = new UserModel();
        userModel.userLogin(usernameview.getText().toString().trim(), passwordview.getText().toString().trim(), this);
    }


    @Override
    public void onSuccess(LoginBean userid, int flag) {

        if (userid.getUserid().equals("0")) {
            Toast.makeText(LoginActivity.this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
        } else {
            saveUser(userid.getUserid());
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onFail() {
        Toast.makeText(LoginActivity.this, "网络出错了", Toast.LENGTH_SHORT).show();
    }

    public void saveUser(String user_id) {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("username", usernameview.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id", userid);
        editor.putString("userpass", passwordview.getText().toString());
        editor.commit();

    }


@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
        {
    super.onActivityResult(requestCode,resultCode,data);
    String et_user=data.getStringExtra("user");
    usernameview.setText(et_user);
}}